package o;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class btu {
    private static final List<bud> ˊ = Arrays.asList(new bud[]{new btz()});
    private static final List<bud> ˋ = Arrays.asList(new bud[]{new btz(), new btt(), new btx()});

    public static SpannableStringBuilder ॱ(Context context, String str, List<bud> list) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        if (str == null || str.isEmpty()) {
            return spannableStringBuilder;
        }
        if (list == null || list.isEmpty()) {
            return spannableStringBuilder.append(str);
        }
        List arrayList = new ArrayList();
        do {
            ˎ(str, list, arrayList);
            if (arrayList.size() == 0) {
                spannableStringBuilder.append(str);
                break;
            }
            Collections.sort(arrayList);
            bty o_bty = (bty) arrayList.get(0);
            spannableStringBuilder.append(str.substring(0, o_bty.ˊ()));
            CharSequence ॱ = o_bty.ॱ(context, str.substring(o_bty.ˊ(), o_bty.ˎ()));
            if (ॱ != null) {
                spannableStringBuilder.append(ॱ);
            } else {
                spannableStringBuilder.append(str.substring(o_bty.ˊ(), o_bty.ˎ()));
            }
            str = str.substring(o_bty.ˎ());
        } while (!str.isEmpty());
        return spannableStringBuilder;
    }

    private static void ˎ(String str, List<bud> list, List<bty> list2) {
        list2.clear();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            bty ˎ = ((bud) it.next()).ˎ(str);
            if (ˎ != null) {
                list2.add(ˎ);
            } else {
                it.remove();
            }
        }
    }

    public static StringBuilder ˏ(EditText editText) {
        int i = 0;
        String obj = editText.getText().toString();
        CharacterStyle[] characterStyleArr = (CharacterStyle[]) editText.getText().getSpans(0, editText.getText().toString().length(), CharacterStyle.class);
        StringBuilder stringBuilder = new StringBuilder();
        try {
            int length = characterStyleArr.length;
            int i2 = 0;
            while (i2 < length) {
                int i3;
                Object obj2 = characterStyleArr[i2];
                if (obj2 == null) {
                    i3 = i;
                } else if (obj2 instanceof btv) {
                    i3 = i;
                } else {
                    int spanStart = editText.getText().getSpanStart(obj2);
                    int spanEnd = editText.getText().getSpanEnd(obj2);
                    stringBuilder.append(obj.substring(i, spanStart));
                    if (obj2 instanceof btn) {
                        stringBuilder.append(((btn) obj2).ॱ(obj, spanStart, spanEnd));
                        i3 = spanEnd;
                    } else {
                        i3 = spanStart;
                    }
                }
                i2++;
                i = i3;
            }
        } catch (Exception e) {
            bqx.ˋ("UbbContentFormat", "formatContentString error");
        }
        stringBuilder.append(obj.substring(i));
        return stringBuilder;
    }

    public static List<bud> ˏ() {
        List<bud> arrayList = new ArrayList();
        arrayList.addAll(ˊ);
        return arrayList;
    }

    public static List<bud> ॱ() {
        List<bud> arrayList = new ArrayList();
        arrayList.addAll(ˋ);
        return arrayList;
    }
}
